<template>
  <div>
    <div>【服务资源占用比】</div>
    <div ref="target" class="w-full h-full"></div>
  </div>
</template>
<script lang="ts" setup>
import { ref, defineProps, onMounted, watch } from "vue";
import * as echarts from "echarts";
import type { EChartsOption, BarSeriesOption, EChartsType } from "echarts"; // 导入类型
import type { Servers, Server } from "@/api/types";

const props = defineProps<{
  data: Servers;
}>();
console.log("竖向柱状图", props.data);
const target = ref<HTMLElement | null>(null);

let mChart: EChartsType;
onMounted(() => {
  mChart = echarts.init(target.value as HTMLElement);
  renderChart();
});

const renderChart = () => {
  if (props.data.servers.length === 0) return;
  const options: EChartsOption = {
    //x轴展示数据
    xAxis: {
      type: "category",
      data: props.data.servers.map((item) => item.name),
      axisLabel: {
        color: "#9eb1c8",
      },
    },
    yAxis: {
      show: false,
      type: "value",
      max: function (value: any) {
        return value.max * 1.2;
      },
    },
    // 图表绘制的位置,上下左右
    grid: {
      top: 16,
      bottom: 26,
      left: -26,
      right: 0,
      containLabel: true,
    },
    // 核心配置
    series: [
      {
        type: "bar",
        data: props.data.servers.map((item) => ({
          name: item.name,
          value: item.value,
        })),

        itemStyle: {
          color: "#479AD3",
          borderRadius: 5,
          shadowColor: "rgba(0,0,0,0.3)",
          shadowBlur: 5,
        },
        barWidth: 12,
        label: {
          show: true,
          position: "top",
          color: "#fff",
          formatter: "{c}%",
        },
      },
    ],
  };
  //3 .最后通过 实例.set0ption 方法，设置配置对象
  mChart.setOption(options);
};

watch(() => props.data, renderChart);
</script>
<style scoped lang="scss"></style>
